package com.liukai.todo_android.common.utils;

import android.os.Build;

import androidx.annotation.RequiresApi;

import java.text.SimpleDateFormat;
import java.time.Instant;
import java.time.LocalDateTime;
import java.time.ZoneId;
import java.time.ZonedDateTime;
import java.util.Date;

/**
 * @author ：liukai
 * @date ：Created in 2021/8/6 4:55 下午
 * @description：日期格式转换
 * @modified By：
 * @version: 1.0.0$
 */
public class DateUtil {

    /**
     * Date类型转LocalDateTime类型
     *
     * @param date 日期
     * @return LocalDateTime
     */
    @RequiresApi(api = Build.VERSION_CODES.O)
    public static LocalDateTime DateToLocalDateTime(Date date) {
        Instant instant = date.toInstant();
        ZoneId zoneId = ZoneId.systemDefault();
        LocalDateTime localDateTime = instant.atZone(zoneId).toLocalDateTime();
        System.out.println("Date = " + date);
        System.out.println("LocalDateTime = " + localDateTime);
        return localDateTime;
    }

    /**
     * LocalDateTime转Date类型
     *
     * @param localDateTime LocalDateTime
     * @return Date
     */
    @RequiresApi(api = Build.VERSION_CODES.O)
    public static Date LocalDateTimeToDate(LocalDateTime localDateTime) {
        ZoneId zoneId = ZoneId.systemDefault();
        ZonedDateTime zdt = localDateTime.atZone(zoneId);

        Date date = Date.from(zdt.toInstant());

        System.out.println("LocalDateTime = " + localDateTime);
        System.out.println("Date = " + date);
        return date;
    }

    /**
     * 根据日期获取年月日
     *
     * @param date
     * @return yyyy-MM-dd
     */
    public static String getYearMonthDay(Date date) {
        SimpleDateFormat sp = new SimpleDateFormat("yyyy-MM-dd");
        return sp.format(date);
    }

}
